package com.volcreate.inn.controller.admin;


import com.volcreate.inn.po.User;
import com.volcreate.inn.service.impl.BlogService;
import com.volcreate.inn.service.impl.TagService;
import com.volcreate.inn.service.impl.TypeService;
import com.volcreate.inn.utils.MyStringUtils;
import com.volcreate.inn.vo.BlogQuery;
import com.volcreate.inn.vo.BlogVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author zxc
 * @version 0.0.1
 * @date 2020/8/20
 */
@Controller
@RequestMapping("/admin")
public class ABlogController {

    private static final String EDIT = "admin/blog-edit";
    private static final String LIST = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";
    private static final String SEARCH = "admin/blogs :: blogList";


    private final BlogService blogService;
    private final TypeService typeService;
    private final TagService tagService;

    public ABlogController(BlogService blogService, TypeService typeService, TagService tagService) {
        this.blogService = blogService;
        this.typeService = typeService;
        this.tagService = tagService;
    }

    @GetMapping("/blogs")
    public String blogs(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                        HttpSession session,
                        BlogQuery blogQuery, Model model) {
        // 只需要刷新type
        blogQuery.setSortBy("gmt_modified");
        User user = (User) session.getAttribute("user");
        if (user.getType() <= 2) {
            model.addAttribute("types", typeService.list());
            model.addAttribute("page", blogService.listByBlogQuery(page, 8, blogQuery));
        } else {
            model.addAttribute("types", typeService.list());
            model.addAttribute("page", blogService.listBlogByUser(page, 8, user.getId()));
        }
        return LIST;
    }


    @PostMapping("/blogs/search")
    public String search(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                         BlogQuery blogQuery, Model model) {
        blogQuery.setSortBy("gmt_modified");
        model.addAttribute("page", blogService.listByBlogQuery(page, 8, blogQuery));
        // 返回一个片段，实现局部刷新
        return SEARCH;
    }

    @GetMapping("/blogs/edit")
    public String edit(Model model) {
        // 普通的新增，没有带id
        setTypeAndTag(model);
        model.addAttribute("blog", new BlogVO());
        return EDIT;
    }

    @GetMapping("/blogs/{id}/edit")
    public String modify(@PathVariable Long id, Model model) {
        // 从 modify 去往 EDIT 的blog带有id，再次向post提交时，就可以用来判断是否是修改。
        setTypeAndTag(model);
        BlogVO blog = blogService.getOne(id);
        blog.init();
        model.addAttribute("blog", blog);
        return EDIT;
    }

    private void setTypeAndTag(Model model) {
        model.addAttribute("types", typeService.list());
        model.addAttribute("tags", tagService.list());
    }


    @PostMapping("/blogs")
    public String post(BlogVO blog, RedirectAttributes attributes, HttpSession session) {
        User user = (User) session.getAttribute("user");
        blog.setUserId(user.getId());
        blog.setUser(user);

        blog.setTags(tagService.list(blog.getTagIds()));
        blog.setDescription(MyStringUtils.stringCut(blog.getDescription(), 252));

        boolean b;
        if (blog.getId() == null) {
            b = blogService.save(blog);
        } else {
            // 这里提交的blog对象中，有一些内容为空，比如说createTime和views
            b = blogService.updateById(blog.getId(), blog);
        }
        if (b) {
            attributes.addFlashAttribute("message", "操作失败");
        } else {
            attributes.addFlashAttribute("message", "操作成功");
        }
        return REDIRECT_LIST;
    }

    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        blogService.deleteById(id);
        attributes.addFlashAttribute("message", "删除成功");
        return REDIRECT_LIST;
    }
}
